package com.mcp.server.handler;

import java.util.HashMap;
import java.util.Map;

/**
 * 工具方法处理器
 */
public class ToolHandler {
    private final Map<String, ToolFunction> tools = new HashMap<>();
    
    public ToolHandler() {
        // 注册内置工具
        registerTool("add", params -> {
            if (params instanceof Map<?, ?> map) {
                Number a = (Number) map.get("a");
                Number b = (Number) map.get("b");
                return a.doubleValue() + b.doubleValue();
            }
            throw new IllegalArgumentException("参数需要包含a和b");
        });
    }
    
    /**
     * 注册工具方法
     */
    public void registerTool(String name, ToolFunction function) {
        tools.put(name, function);
    }
    
    /**
     * 处理工具调用
     */
    public Object handle(String method, Object params) throws Exception {
        if (!tools.containsKey(method)) {
            throw new Exception(method);
        }
        return tools.get(method).apply(params);
    }
    
    @FunctionalInterface
    public interface ToolFunction {
        Object apply(Object params) throws Exception;
    }
}